package com.beiming.libra.controller;

import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.page.PageResult;
import com.beiming.libra.domain.UserMessage;
import com.beiming.libra.helper.Result;
import com.beiming.libra.service.UserMessageService;
import com.google.common.base.Optional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: tyrion
 * @Date: 2018/6/6 11:49
 * @类描述: 用户消息控制层
 */
@RestController
@RequestMapping("/api/um")
@Api(value = "用户消息接口", tags = "用户消息接口")
public class UserMessageController {

  @Autowired
  UserMessageService userMessageService;


  @GetMapping(value = "/getOne/{id}")
  @ApiOperation(value = "获取消息详情", notes = "获取消息详情")
  public Result getMessage(
      @ApiParam(value = "消息主id", required = true) @PathVariable("id") String id) {
    UserMessage userMessage = userMessageService.getMessageById(id);
    Optional<UserMessage> message = Optional.fromNullable(userMessage);
    if (message.isPresent()) {
      return Result.success(userMessage);
    }
    return Result.failed(ErrorCode.RESULT_EMPTY,
        "消息为空");
  }

  @GetMapping(value = "/setRead/{id}")
  @ApiOperation(value = "设置为已读", notes = "设置为已读")
  public Result setRead(@ApiParam(value = "消息id", required = true) @PathVariable("id") String id) {
    userMessageService.updateStatusById(id);
    return Result.success();
  }


  // 分页处理。。。。。。
  @PostMapping(value = "/listMessage")
  @ApiOperation(value = "获取消息列表", notes = "获取消息列表")
  public Result<PageResult<UserMessage>> listWithCondition(UserMessage um) {
    PageResult<UserMessage> listMessages = userMessageService.listWithCondition(um);
    if (listMessages.getList().size() == 0) {
      return Result.failed(ErrorCode.RESULT_EMPTY, "数据为空");
    }
    return Result.success(listMessages);
  }


  @PostMapping(value = "/saveMessage")
  @ApiOperation(value = "插入消息", notes = "插入消息")
  public Result insertUserMessage(UserMessage um) {
    userMessageService.insertUserMessage(um);
    return Result.success();
  }


}
